<?php

namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;

class MicroBlogRepository
{
    /**
     * 动态删除
     */
    public function delete(int $id)
    {
        $detail = app('model_microblog')
            ->where([
                'id' => $id,
                'status' => 1
            ])
            ->first();
        if (empty($detail)) throw new BasicException(70101);
        $detail->status = 2;
        $detail->save();

        return [];
    }

    /**
     * 人工审核
     */
    public function check($ids, int $verify)
    {
        $ids = explode(',', $ids);
        foreach ($ids as $id) {
            if ($id) {
                $detail = app('model_microblog')
                    ->where([
                        'id' => $id,
                        'status' => 1,
                        ['verify', '<=', 2]
                    ])->first();
                if ($detail) {
                    $detail->verify = $verify;
                    $detail->save();
                } else {
                    throw new BasicException(0, "记录不存在或者已被审核，ID:{$id}");
                }
            }
        }
        return true;
    }

    /**
     * 获取动态列表
     */
    public function list(int $page, int $perPage, int $verify, int $userCode)
    {
        $query = app('model_microblog')
            ->from('ln_microblog as base')
            ->join('ln_user_data as ud', 'base.user_id', 'ud.user_id')
            ->where('base.status', 1)
            ->select('base.id','base.cover','base.content','base.img_arr','base.add_time','base.verify','base.type','ud.nickname','ud.head_picture','ud.user_code');
        if ($verify) {
            $query = $query->where('verify', $verify);
        } else {
            $query = $query->where('verify', '<=', 2);
        }
        if ($userCode) $query = $query->where('ud.user_code', $userCode);
        $list = $query->orderBy('base.id', 'desc')->paginate($perPage, ['*'], '', $page)->toArray();
        if (!empty($list['data'])) {
            foreach ($list['data'] as $key => $value) {
                $temp = json_decode($value['cover'],true);
                $list['data'][$key]['img_arr'] = json_decode($value['img_arr'], true);
                $list['data'][$key]['add_time'] = date('m-d H:i', $value['add_time']);
                $list['data'][$key]['cover'] = $temp[0]['url'] ?? '';
            }
        }

        return [
            'total' => $list['total'],
            'current_page' => $list['current_page'] ?? 1,
            'last_page_number' => $list['last_page'] ?? 1,
            'list' => $list['data'] ?? []
        ];
    }

    //审核通过的动态
    public function listPassed(int $page, int $perPage, int $verify, int $userCode,$params)
    {
        $start_date = empty($params['start_date']) ? '' : strtotime($params['start_date']);
        $end_date = empty($params['end_date']) ? '' : strtotime($params['end_date'] . " 23:59:59");

        $query = app('model_microblog')
            ->from('ln_microblog as base')
            ->join('ln_user_data as ud', 'base.user_id', 'ud.user_id')
            ->where('base.status', 1);
        //时间 条件拼接
        if (empty($start_date) && !empty($end_date)) {
            $query->where('base.add_time', '<=', $end_date);
        } elseif (!empty($start_date) && empty($end_date)) {
            $query->where('base.add_time', '>=', $start_date);
        } elseif (!empty($start_date) && !empty($end_date)) {
            $query->whereBetween('base.add_time', [$start_date, $end_date]);
        }

        if (!empty($params['nickname'])) {
            $query->where('ud.nickname', 'like', "%" . $params['nickname'] . "%");
        }

        $query->select('base.id','base.cover','base.content','base.img_arr','base.add_time','base.verify','base.type','ud.nickname','ud.head_picture','ud.user_code');
        if ($verify) {
            $query = $query->where('verify', $verify);
        } else {
            $query = $query->where('verify', '<=', 2);
        }
        if ($userCode) $query = $query->where('ud.user_code', $userCode);
        $list = $query->orderBy('base.id', 'desc')->paginate($perPage, ['*'], '', $page)->toArray();
        if (!empty($list['data'])) {
            foreach ($list['data'] as $key => $value) {
                $temp = json_decode($value['cover'],true);
                $list['data'][$key]['img_arr'] = json_decode($value['img_arr'], true);
                $list['data'][$key]['add_time'] = date('m-d H:i', $value['add_time']);
                $list['data'][$key]['cover'] = $temp[0]['url'] ?? '';
            }
        }

        return [
            'total' => $list['total'],
            'current_page' => $list['current_page'] ?? 1,
            'last_page_number' => $list['last_page'] ?? 1,
            'list' => $list['data'] ?? []
        ];
    }

    //删除评论
    public function deleteComment($id)
    {
        $comment = app('model_microblog_comment')->where('id', $id)->first();
        if (empty($comment) || $comment->status != 1) {
            throw new BasicException(96003);
        }

        DB::beginTransaction();
        try {
            //修改评论状态为删除2
            app('model_microblog_comment')->where('id', $id)->update(['status' => 2]);
            //评论量-1
            app('model_microblog')->where('id', $comment->blog_id)->decrement('comment_num');
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw new BasicException(96004);
        }
    }


    /**
     * Notes:获取评论列表
     * User: wangyueli
     *
     * @param  int  $page
     * @param  int  $perPage
     * @param $blog_id
     * @return array
     */
    public function getCommentList(int $page, int $perPage,$blog_id)
    {
        $query = app('model_microblog_comment')
            ->from('ln_microblog_comment as base')
            ->join('ln_user_data as ud', 'base.user_id', 'ud.user_id')
            ->where('base.status', 1)
            ->where('base.blog_id', $blog_id)
            ->select('base.id','base.user_id','base.content','base.add_time','ud.nickname','ud.user_code');

        $list = $query->orderBy('base.id', 'desc')->paginate($perPage, ['*'], '', $page)->toArray();

        return [
            'total' => $list['total'],
            'list' => $list['data'] ?? []
        ];
    }

}


